﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XNALibrary.Interfaces;

namespace XNALibrary.Effects
{
    public class ZCustomEffect: CustomEffect
    {
        public ZCustomEffect(Effect e)
            : base(e)
        {

        }

        public override void SetupParameters()
        {
        }

        public override void Update()
        {
            //do nothing
        }

        public override void DrawEffect(RigidBody body, RenderControl rc)
        {
            Vector3 CamPos = rc.CurrentCamera.GetPosition();
            Matrix view = rc.CurrentCamera.View;
            Matrix projection = rc.CurrentCamera.Projection;

            IPointLight[] lights = rc.GetLights(body.Position, 4);
            switch (lights.Length)
            {
                case 4:
                    //Light 4
                    effect.Parameters["Light4Enabled"].SetValue(lights[3].IsOn);
                    effect.Parameters["LightPos4"].SetValue(lights[3].Position);
                    effect.Parameters["LightPower4"].SetValue(lights[3].Intensity);
                    effect.Parameters["Color4"].SetValue(lights[3].Color.ToVector4());
                    goto Light3;

                case 3:
                    //Light 3
                    effect.Parameters["Light4Enabled"].SetValue(false);
                Light3:
                    //Light 3
                    effect.Parameters["Light3Enabled"].SetValue(lights[2].IsOn);
                    effect.Parameters["LightPos3"].SetValue(lights[2].Position);
                    effect.Parameters["LightPower3"].SetValue(lights[2].Intensity);
                    effect.Parameters["Color3"].SetValue(lights[2].Color.ToVector4());
                    goto Light2;

                case 2:
                    effect.Parameters["Light4Enabled"].SetValue(false);
                    effect.Parameters["Light3Enabled"].SetValue(false);
                Light2:
                    //Light 2
                    effect.Parameters["Light2Enabled"].SetValue(lights[1].IsOn);
                    effect.Parameters["LightPos2"].SetValue(lights[1].Position);
                    effect.Parameters["LightPower2"].SetValue(lights[1].Intensity);
                    effect.Parameters["Color2"].SetValue(lights[1].Color.ToVector4());
                    goto Light1;

                case 1:
                    effect.Parameters["Light4Enabled"].SetValue(false);
                    effect.Parameters["Light3Enabled"].SetValue(false);
                    effect.Parameters["Light2Enabled"].SetValue(false);
                Light1:
                    //Light 1
                    effect.Parameters["Light1Enabled"].SetValue(lights[0].IsOn);
                    effect.Parameters["LightPos1"].SetValue(lights[0].Position);
                    effect.Parameters["LightPower1"].SetValue(lights[0].Intensity);
                    effect.Parameters["Color1"].SetValue(lights[0].Color.ToVector4());

                    //If there are lights, Enable Lighting
                    effect.Parameters["LightEnabled"].SetValue(true);
                    break;

                case 0:
                    effect.Parameters["LightEnabled"].SetValue(false);
                    break;
            }

            effect.Parameters["time"].SetValue(0f);
            effect.Parameters["height"].SetValue(0.05f);
            effect.Parameters["Direction"].SetValue(2);
            effect.Parameters["WaveEnabled"].SetValue(false);

            effect.Parameters["World"].SetValue(body.GetWorld(body.Model.Meshes[0]));
            effect.Parameters["Texture"].SetValue(ModelTexture);
            effect.Parameters["TextureEnabled"].SetValue(ModelTexture == null ? false : true);
            effect.Parameters["View"].SetValue(view);
            effect.Parameters["Projection"].SetValue(projection);

            foreach (ModelMesh mesh in body.Model.Meshes)
            {
                // set the vertex source to the mesh's vertex buffer
                rc.graphicsDevice.Vertices[0].SetSource(
                    mesh.VertexBuffer, mesh.MeshParts[0].StreamOffset, mesh.MeshParts[0].VertexStride);

                // set the vertex declaration
                rc.graphicsDevice.VertexDeclaration = mesh.MeshParts[0].VertexDeclaration;

                // set the current index buffer to the sample mesh's index buffer
                rc.graphicsDevice.Indices = mesh.IndexBuffer;
                
                effect.Begin();
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    rc.graphicsDevice.DrawIndexedPrimitives(
                    PrimitiveType.TriangleList, mesh.MeshParts[0].BaseVertex, 0,
                    mesh.MeshParts[0].NumVertices, mesh.MeshParts[0].StartIndex, mesh.MeshParts[0].PrimitiveCount);
                    pass.End();
                }
                effect.End();
            }
        }
    }
}
